package com.ssm.SSM_test.service.impl;

import com.ssm.SSM_test.domain.Account;
import com.ssm.SSM_test.mapper.AccountMapper;
import com.ssm.SSM_test.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void save(Account account){
//        InputStream inputStream = null;
//        try {
//            inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            SqlSession sqlSession = sessionFactory.openSession();
//            AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//            System.out.println(mapper);
//            mapper.save(account);
//            sqlSession.commit();
////            事务未提交
//            sqlSession.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    accountMapper.save(account);
    }
    @Override
    public List<Account> findAll(){
//        try {
//            InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            SqlSession sqlSession = sessionFactory.openSession();
//            AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//            List<Account> list = mapper.findAll();
//            sqlSession.close();
//            return list;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
            List<Account> list = accountMapper.findAll();
            return list;
    }
}
